package com.itboye.lottery.activity.user;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.handmark.pulltorefresh.library.ObservableScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.ZhuiHaoDetailActivity;
import com.itboye.lottery.adapter.ZhuiHaoManageAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.BetBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class ZhuihaoManageActivity extends BaseActivity implements ResponseCallback {

    RecyclerView rv_address_list;
    UserPresenter userPresenter = new UserPresenter(this);
    int page = 1;
    private ArrayList<BetBean> betBeanArrayList = new ArrayList<>();
    private ZhuiHaoManageAdapter adapter;
    PullToRefreshScrollView pull_to_refresh_scrollView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_zhuihao_manage;
    }


    @Override
    protected void initData() {
        rv_address_list.setLayoutManager(new LinearLayoutManager(this));
        pull_to_refresh_scrollView.setMode(PullToRefreshBase.Mode.BOTH);
        pull_to_refresh_scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ObservableScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                page = 1;
                getData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                page++;
                getData();
            }
        });
        adapter = new ZhuiHaoManageAdapter(this, betBeanArrayList);
        adapter.setOnItemClickListener((view, position) -> {
            startActivity(new Intent(ZhuihaoManageActivity.this, ZhuiHaoDetailActivity.class)
                    .putExtra("id", betBeanArrayList.get(position).getId()));
        });
        rv_address_list.setAdapter(adapter);
        getData();
    }

    private void getData() {
        userPresenter.getZhuiHaoList(page);
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        ByAlert.alert(msg);
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        ArrayList betBeanArrayListTemp = (ArrayList<BetBean>) o;
        if (page == 1) {
            betBeanArrayList.clear();
        }
        if (betBeanArrayListTemp!=null) {
            betBeanArrayList.addAll(betBeanArrayListTemp);
            adapter.notifyDataSetChanged();
        }
    }
}
